package sbc.group1.lab1.alt;


import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import sbc.group1.lab1.common.Car;
import sbc.group1.lab1.common.TestStatus;
import sun.misc.Signal;
import sun.misc.SignalHandler;


public class TesterCompleteness extends AbstractTester {

	public TesterCompleteness() throws MalformedURLException, RemoteException,
			NotBoundException {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 * @throws NotBoundException 
	 * @throws IOException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws NotBoundException, IOException, InterruptedException {
		TesterCompleteness tester = new TesterCompleteness();

        final Thread t = new Thread(tester,"run-alt-completeness");
        Signal.handle(new Signal("INT"), 
            new SignalHandler() {
                @Override
                public void handle(Signal sig) {
                    t.interrupt();
                }
            });
        
        if (System.getProperty("benchmark") != null && System.getProperty("benchmark").equals("true")) {
            Signal.handle(new Signal("HUP"), 
                    new SignalHandler() {
                        @Override
                        public void handle(Signal sig) {
                            t.start();
                        }
                    });
            System.in.read();
        }else {
            t.start();
            System.in.read();
            t.interrupt();
            t.join();
        }

	}
	

	@Override
	protected void doWork() throws RemoteException, InterruptedException {
		Car car = null;

		car = cs.takeCarForCompletenessTester();

		
		if (!car.isComplete()) {
            car.markAsDefect();
            car.setCompletenessTestStatus(TestStatus.FAILED);        
        }else {
            car.setCompletenessTestStatus(TestStatus.PASSED);
        }
		if(car.isCompletelyTested()){
        	cs.addCompleteTestedCar(car);
        }
        else{
        	cs.addForCompletenessTester(car);
        }
	}
	
	
}
